/*----------------------------------------------------------------
 * \file        ct_voip_msg_cache.h
 * \author      lilian lilian@hujiang.com
 * \date        2014/08/08
 * \brief
 *      cache for ctVOIPMsg.
 *
 * Stream cache, each stream has a cache object.
 * The cache will holding about packets in latest 2 seconds, in case client want to resend a packet due to 
 * UDP packet lossless or other purpose
 *
 * The cache should be a ring buffer, new packets will replace old packets automatically.
 *----------------------------------------------------------------*/

#ifndef CTVOIP_MSG_CACHE_H_
#define CTVOIP_MSG_CACHE_H_

#include "ctTime.h"
#include "command.h"
#include <map>

class ctVOIPMsg;

class ctVOIPMsgCache {
        public:
                ctVOIPMsgCache(int max=30);
                ~ctVOIPMsgCache();

        public:
                void            add(uint32_t seq, ctVOIPMsg* msg);
                ctVOIPMsg*      find(uint32_t seq);
                void            clear();
                bool            checkTimeout(const ctRTime& curtime) const;

        private:
                static const unsigned long CACHE_TIMEOUT_MS = 60*1000; //60s
                typedef std::map<int, ctVOIPMsg*>	ctVOIPMsgMap;

        private:
                int	        max_size_;
                ctRTime         last_msg_time_;
                ctVOIPMsgMap    msg_map_;
};

#endif //CTVOIP_MSG_CACHE_H_
